<?php
defined('BY_JYA') or exit('error');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$setting=$this->plugin_setting('setting');
$url = 'https://oauth.open.weidian.com/token?grant_type=client_credential&appkey='.$setting['appkey'].'&secret='.$setting['secret'];
$data = cfc('http')->ihttp_request($url);
$data=json_decode($data,true);
$result=json_decode($data['result'],true);
$access_token=$result['access_token'];
if($_RQ['op']=='list'){
	$params=array(
		'uniacid'=>$_SESSION['uniacid'],
		'page_num'=>1,
		'page_size'=>1,
		'order_type'=>'finish',
		'add_start'=>'2014-11-12 16:36:08',
		'add_end'=>'2014-11-12 16:36:08',
		'is_wei_order'=>0,
		'group_type'=>1,
	);
	$public=array(
		'method'=>"vdian.order.list.get",
		'access_token'=>$access_token,
		'version'=>'1.2'
	);
	if($_RQ['title']){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	$params=json_encode($params);
	$public=json_encode($public);
	$orderurl='https://api.vdian.com/api?param='.$params.'&public='.$public;
	$orderdata = cfc('http')->ihttp_request($orderurl);
	$orderdata=json_decode($orderdata,true);
	$list=json_decode($orderdata['orders'],true);
	$list['buyer_info']=json_decode($list['buyer_info'],true);
	if($list){
		foreach($list as &$item){
			$item['name']=$item['buyer_info']['name'];
		}
	}
	$return['list']=$list?$list:array();
	/*$orderby=array();
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('core_account_message',$params,array($pindex,$psize),$total,$fields,'','id desc');
	$return['list']=$list?$list:array();
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	*/
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'订单列表'),
			//array('op'=>'formlist','title'=>'自定义表单列表'),
		),
		'list'=>array('data'=>'list','edit'=>false,'key'=>'order_id','list'=>array(
			array('name'=>'order_id','title'=>'订单ID','type'=>'string'),
			array('name'=>'name','title'=>'买家姓名','type'=>'string'),
			array('name'=>'phone','title'=>'买家电话','type'=>'string'),
			array('name'=>'total','title'=>'订单总价','type'=>'string'),
			array('name'=>'img','title'=>'商品图','type'=>'image'),
			array('name'=>'time','title'=>'下单时间','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
			array('name'=>'express_no','title'=>'快递单号','type'=>'string'),
		),       'action' => 
		    array (
			    
		           array ('title' => '订单详情','type' => 'modal','op'=>'detail'),
		           array ('title' => '取消订单','type' => 'post','op'=>'cancel'),
		           array ('title' => '发货','type' => 'post','op'=>'cancel'),
				   ),),
		'search_one'=>array('name'=>'username','help'=>'客户姓名','add'=>false),
	);
	$return['htmls']=$htmls;
	$return['status']=array('10'=>'待付款','20'=>'已付款','21'=>'部分付款','30'=>'已发货','31'=>'部分发货','40'=>'已收货','50'=>'已完成','60'=>'已关闭');
	exi($return);
}
if($_RQ['op']=='detail'){
	$order_id=intval($_RQ['order_id']);
	if($order_id){
		$params=array(
		'order_id'=>$order_id,
	);
	$public=array(
		'method'=>"vdian.order.get",
		'access_token'=>$access_token,
		'version'=>'1.0',
		'format'=>'json'
	);
	$params=json_encode($params);
	$public=json_encode($public);
	$detailurl='https://api.vdian.com/api?param='.$params.'&public='.$public;
	$detaildata = cfc('http')->ihttp_request($detailurl);
	$detaildata=json_decode($detaildata,true);
	$item=json_decode($detaildata['result'],true);
	$item['buyer_info']=json_decode($item['buyer_info'],true);
	
	}else{
		$item=array();
	}
	if($item){
		foreach($item as &$row){
			$row['name']=$item['buyer_info']['name'];
			$row['phone']=$item['buyer_info']['phone'];
			$row['address']=$item['buyer_info']['address'];
		}
	}
	$return['item']=$item;
	 $htmls=array(
		'type'=>'look',
		'desc'=>'订单详情',
		'look'=>array(
			array('type'=>'string','title'=>'买家姓名','name'=>'name','read'=>true),
			array('type'=>'string','title'=>'收货人电话','name'=>'phone','read'=>true),
			array('type'=>'string','title'=>'收货地址','name'=>'address','read'=>true),
			array('type'=>'string','title'=>'订单总金额','name'=>'total','read'=>true),
			array('type'=>'radio','title'=>'订单状态','name'=>'status_ori','options'=>'status_ori','read'=>true),
		)
	);
	$return['htmls']=$htmls;
	$return['status_ori']=array('10'=>'待付款','20'=>'已付款','21'=>'部分付款','30'=>'已发货','31'=>'部分发货','40'=>'已收货','50'=>'已完成','60'=>'已关闭');
	exi($return);
}

if($_RQ['op']=='cancel'){
	$order_id=intval($_RQ['order_id']);
	if($order_id){
		$params=array(
		'order_id'=>$order_id,
		'reason'=>'取消订单',
	);
	$public=array(
		'method'=>"vdian.order.cancel",
		'access_token'=>$access_token,
		'version'=>'1.0',
		'format'=>'json'
	);
	$params=json_encode($params);
	$public=json_encode($public);
	$cancelurl='https://api.vdian.com/api?param='.$params.'&public='.$public;
	$canceldata = cfc('http')->ihttp_request($cancelurl);
	$canceldata=json_decode($canceldata,true);
	$status=json_decode($canceldata['status'],true);
	if($status['status_code']==0){
		exi('订单取消成功');
	}else{
		exi('订单取消失败：'.$status['status_reason'],'error');
	}
	}else{
		exi('订单不存在，请重新选择','error');
	}
}